package com.yxzx.ebs.controller;


import com.yxzx.common.YxzxResult;
import com.yxzx.ebs.entity.EbsChapter;
import com.yxzx.ebs.entity.vo.EbsChapterVo;
import com.yxzx.ebs.service.EbsChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author kongfuzi
 * @since 2020-07-31
 */
@Api(tags = "课程章节管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/eduservice/chapter")
public class EbsChapterController {
    @Autowired
    private EbsChapterService chapterService;

    @ApiOperation("查询章节和小节数据列表")
    @GetMapping("/chaterAndVedio/{courseId}")
    public YxzxResult getChapterAndVedioList(
            @ApiParam(name = "courseId",value = "课程ID",required = true)
            @PathVariable  String courseId){
        List<EbsChapterVo> chapterAndVedioList = chapterService.getChapterAndVedioList(courseId);
        return YxzxResult.ok().data("items",chapterAndVedioList);
    }

    @ApiOperation("新增章节")
    @PostMapping("saveChapter")
    public YxzxResult save(@ApiParam(name = "EbsChapter", value = "章节对象",required = true)
                           @RequestBody EbsChapter ebsChapter){
        boolean save = chapterService.save(ebsChapter);
        return save ? YxzxResult.ok() : YxzxResult.error();
    }

    @ApiOperation("根据ID查询章节")
    @GetMapping("{id}")
    public YxzxResult getById(@ApiParam(name = "id", value = "章节ID", required = true)
                              @PathVariable String id){
        EbsChapter chapter = chapterService.getById(id);
        return YxzxResult.ok().data("item",chapter);
    }

    @ApiOperation("更新章节信息")
    @PutMapping("updateChapter")
    public YxzxResult updateById(@ApiParam(name = "EbsChapter", value = "章节对象", required = true)
                                 @RequestBody EbsChapter ebsChapter){
        boolean b = chapterService.updateById(ebsChapter);
        return  b ? YxzxResult.ok() : YxzxResult.error();
    }

    @ApiOperation("删除章节信息")
    @DeleteMapping("{id}")
    public YxzxResult deleteById(@ApiParam(name = "id", value = "章节ID", required = true)
                                     @PathVariable String id){
        log.info("删除章节信息: "+ id);
        chapterService.removeChapterById(id);
        return YxzxResult.ok();
    }
}

